#include "taghandler.h"
#include <QDebug>

TagHandler::TagHandler()
{
    isPathTemp = false;
    tmpPath = "tmpFile.";
}

bool TagHandler::loadPath(QString path)
{
    if(path != "")
    {
    QFileInfo filei(path);
    this->path = filei.absoluteFilePath();
    //qDebug()<<"filei"<<filei.suffix();

    if(filei.exists())
    {
        //qDebug() << "exists";
        if(this->path == this->path.toUtf8().data())
        {
        if(filei.suffix().toLower() == "mp4" ||
            filei.suffix().toLower() == "avi" ||
            filei.suffix().toLower() == "mpeg" ||
            filei.suffix().toLower() == "mpg" ||
            filei.suffix().toLower() == "wmv"
            )
        {
            return false;
        }
        tmpPath = this->path;
        return true;
        }
        else
        {
        //qDebug() << "not the same";
        QFileInfo *fileInfo = new QFileInfo(path);
        QString suff = fileInfo->suffix().toLower();
        tmpPath+=suff;
        file.setFileName(path);
        QDir::setCurrent("/tmp");

        if(file.copy(tmpPath))
        {
            //qDebug() << "tmpPath:" << tmpPath;
            isPathTemp = true;
            return true;
        }
        else
        {
            //qDebug() << "path" << path;
            return false;
        }
        }
    }
    }

    return false;
}

QString TagHandler::getTitle()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return TStringToQString(tag->title());
}

QString TagHandler::getArtist()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return TStringToQString(tag->artist());
}

QString TagHandler::getAlbum()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return TStringToQString(tag->album());
}

QString TagHandler::getAlbumArtist()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPE2"];
    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

QString TagHandler::getGenre()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return TStringToQString(tag->genre());
}

QString TagHandler::getComposer()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TCOM"];

    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

QString TagHandler::getLabel()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPUB"];

    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

int TagHandler::getYear()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return tag->year();
}

int TagHandler::getTrack()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();
    return tag->track();
}

int TagHandler::getLength()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    properties = f.audioProperties();
    return properties->length();
}

int TagHandler::getBitrate()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    properties = f.audioProperties();
    return properties->bitrate();
}

QString TagHandler::getComment()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    tag = f.tag();

    return TStringToQString(tag->comment());
}

QString TagHandler::getDisc()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPOS"];

    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

int TagHandler::getChannel()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    properties = f.audioProperties();
    return properties->channels();
}

int TagHandler::getFreq()
{
    TagLib::FileRef f(tmpPath.toUtf8().data());
    properties = f.audioProperties();
    return properties->sampleRate();
}

QString TagHandler::getBpm()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TBPM"];

    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

QString TagHandler::getLyrics()
{
    TagLib::MPEG::File f(tmpPath.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["USLT:lyrics"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}
    l = f.ID3v2Tag()->frameListMap()["USLT:text"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}
    l = f.ID3v2Tag()->frameListMap()["USLT:lyrics/text"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}
    l = f.ID3v2Tag()->frameListMap()["USLT:Text encoding"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}
    l = f.ID3v2Tag()->frameListMap()["USLT:Language"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}


    l = f.ID3v2Tag()->frameListMap()["USLT"];
    if(!l.isEmpty()){qDebug()<<TStringToQString(l.front()->toString());}


    if(!l.isEmpty())
    {
        return TStringToQString(l.front()->toString());
    }
    }

    return "";
}

void TagHandler::setArtist(QString artist)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setArtist(artist.toStdString());
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setArtist(artist.toStdString());
    f.save();
    }
}

void TagHandler::setTitle(QString title)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setTitle(title.toStdString());
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setTitle(title.toStdString());
    f.save();
    }
}

void TagHandler::setAlbum(QString album)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setAlbum(album.toStdString());
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setAlbum(album.toStdString());
    f.save();
    }
}

void TagHandler::setAlbumArtist(QString albumArtist)
{
    if(isPathTemp)
    {

    }
    else
    {
    TagLib::MPEG::File f(path.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
        //TagLib::ByteVector bv(albumArtist.toUtf8().data());
        TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPE2"];
        if(!l.isEmpty())
        {
        l.front()->setText(albumArtist.toStdString());

        f.save();
        }
    }
    }
}

void TagHandler::setGenre(QString genre)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setGenre(genre.toStdString());
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setGenre(genre.toStdString());
    f.save();
    }
}

void TagHandler::setComposer(QString composer)
{
    if(isPathTemp)
    {

    }
    else
    {
    TagLib::MPEG::File f(path.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
        //TagLib::ByteVector bv(albumArtist.toUtf8().data());
        TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TCOM"];
        if(!l.isEmpty())
        {
        l.front()->setText(composer.toStdString());
        f.save();
        }
    }
    }
}

void TagHandler::setLabel(QString label)
{
    if(isPathTemp)
    {

    }
    else
    {
    TagLib::MPEG::File f(path.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
        //TagLib::ByteVector bv(albumArtist.toUtf8().data());
        TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPUB"];
        if(!l.isEmpty())
        {
        l.front()->setText(label.toStdString());
        f.save();
        }
    }
    }
}

void TagHandler::setComment(QString comment)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setComment(comment.toStdString());
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setComment(comment.toStdString());
    f.save();
    }
}

void TagHandler::setDisc(QString disc)
{
    if(isPathTemp)
    {

    }
    else
    {
    TagLib::MPEG::File f(path.toUtf8().data());
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
        //TagLib::ByteVector bv(albumArtist.toUtf8().data());
        TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["TPOS"];
        if(!l.isEmpty())
        {
        l.front()->setText(disc.toStdString());
        f.save();
        }
    }
    }
}

void TagHandler::setPuid(QString puid)
{

}

void TagHandler::setLyrics(QString lyrics)
{
    TagLib::MPEG::File f(path.toUtf8().data());
    if(!f.isWritable(path.toUtf8().data()))
    {
    return;
    }
    id3v2tag = f.ID3v2Tag();

    if(id3v2tag)
    {
    //lyrFrame = new TagLib::ID3v2::UnsynchronizedLyricsFrame();
    //lyrFrame->setText(lyrics.toUtf8().data());
    //f.save();

    TagLib::ID3v2::FrameList l = f.ID3v2Tag()->frameListMap()["USLT:Text encoding"];
    l.front()->setText("UTF-8");
    l = f.ID3v2Tag()->frameListMap()["USLT:encoding"];
    l.front()->setText("UTF-8");
    l = f.ID3v2Tag()->frameListMap()["USLT:Language"];
    l.front()->setText("ENG");
    l = f.ID3v2Tag()->frameListMap()["USLT:language"];
    l.front()->setText("eng");
    l = f.ID3v2Tag()->frameListMap()["USLT"];

    if(!l.isEmpty())
    {
        l.front()->setText(lyrics.toStdString());
    }
    f.save();
    }
}

void TagHandler::setTrack(int track)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setTrack(track);
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setTrack(track);
    f.save();
    }
}

void TagHandler::setYear(int year)
{
    if(isPathTemp)
    {
    TagLib::FileRef f(tmpPath.toUtf8().data());
    f.tag()->setYear(year);
    f.save();
    }
    else
    {
    TagLib::FileRef f(path.toUtf8().data());
    f.tag()->setYear(year);
    f.save();
    }
}
